package com.cn.zengzeng.spbzengzeng.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.codec.binary.Base64;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * @Description:
 * @Author: zhangzengzeng
 * @Date: 2021/5/13 0013 11:13
 */
@Slf4j
public class AESUtils {
    private static final String KEY_ALGORITHM = "AES";
    private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";
    public static final String CKEY = "6ay4dlwd4enjbf90";

    // 判断Key是否为16位
    public static byte[] raw = CKEY.getBytes(StandardCharsets.UTF_8);
    public static final  int MODELNUMBER = 16;

    // 加密
    @SuppressWarnings("AlibabaUndefineMagicConstant")
    public static String encrypt (String sSrc) {
        byte[] encrypted;
        try {
            SecretKeySpec skeySpec = new SecretKeySpec(raw,KEY_ALGORITHM);
            //创建密码器
            Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE,skeySpec);
            encrypted = cipher.doFinal(sSrc.getBytes(StandardCharsets.UTF_8));
            // 此处使用BASE64做转码功能，同时能起到2次加密的作用。
            return new Base64().encodeToString(encrypted);
        }catch(NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
            log.error("加密失败,信息{}",e.getMessage());
        }
        return "";
    }

    // 解密
    public static String decrypt (String sSrc,String sKey) {
        try {
            // 判断Key是否正确
            if(sKey == null) {
                log.warn("Key为空null");
                return null;
            }
            // 判断Key是否为16位
            if(sKey.length() % MODELNUMBER != 0) {
                log.warn("Key长度不是16位");
                return null;
            }
            byte[] raw = sKey.getBytes(StandardCharsets.UTF_8);
            SecretKeySpec skeySpec = new SecretKeySpec(raw,"AES");
            Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE,skeySpec);
            // 先用base64解密
            byte[] encrypted1 = new Base64().decode(sSrc);
            byte[] original = cipher.doFinal(encrypted1);
            return new String(original,StandardCharsets.UTF_8);
        }catch(Exception ex) {
            log.error(ex.toString());
            return null;
        }
    }

    public static void main (String[] args) {
        /*
         * 此处使用AES-128-ECB加密模式，key需要为16位。
         */
        String CKEY = "6ay4dlwd4enjbf90";
        // 需要加密的字串
        String cSrc = "123456";
        System.out.println(cSrc);
        // 加密
        String enString = com.cn.zengzeng.spbzengzeng.utils.AESUtils.encrypt(cSrc);
        System.out.println("加密后的字串是：" + enString);

        // 解密
        String deString = com.cn.zengzeng.spbzengzeng.utils.AESUtils.decrypt(enString,CKEY);
        System.out.println("解密后的字串是：" + deString);
    }
    public static String base64ToString(byte[] key) throws Exception {
        return new Base64().encodeToString(key);
    }
}
